package com.mlan.userap.untils;

import android.content.Context;
import android.content.SharedPreferences;

public class SPUtils {

    private static final String DEFAULT_SP_NAME = "default_sp";
    private static SharedPreferences sp;

    /**
     * 初始化 SharedPreferences 实例
     */
    public static void init(Context context) {
        sp = context.getSharedPreferences(DEFAULT_SP_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 存储 String 类型数据
     */
    public static void putString(String key, String value) {
        sp.edit().putString(key, value).apply();
    }

    public static void putLong(String key, Long value) {
        sp.edit().putLong(key, value).apply();
    }

    /**
     * 读取 String 类型数据
     */
    public static String getString(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    public static Long getLong(String key, Long defaultValue) {
        return sp.getLong(key, defaultValue);
    }


    /**
     * 存储 Int 类型数据
     */
    public static void putInt(String key, int value) {
        sp.edit().putInt(key, value).apply();
    }

    /**
     * 读取 Int 类型数据
     */
    public static int getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    /**
     * 存储 Boolean 类型数据
     */
    public static void putBoolean(String key, boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }

    /**
     * 读取 Boolean 类型数据
     */
    public static boolean getBoolean(String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    /**
     * 删除某个键值对
     */
    public static void remove(String key) {
        sp.edit().remove(key).apply();
    }

    /**
     * 清空所有数据
     */
    public static void clear() {
        sp.edit().clear().apply();
    }
}
